local BaseWnd = require(SCRIPTS_DIR .. ".wnds.BaseWnd");
local UiInfo = require(SCRIPTS_DIR .. ".configs.UIInfo");
local Click = require(SCRIPTS_DIR .. ".uikits.Click");

local Direction = {H = 1,V = 2};
local FuncButton = class("FuncButton");
function FuncButton:ctor(args,kind)
    self.kind = kind;
    self.data = args;
    local icon = mgrs.res:getPngPath(args.icon);
    local wnd = args.wnd;
    self.button = display.newSprite(icon);
    local click = Click.new({targetNode = self.button});
    click:clickHandler(function()
        if args.id == 10 or args.id == 1 then
            mgrs.wnd:openWnd(enums.Wnd[wnd]);
        end
        
    end);
end

function FuncButton:expan(t,pos)
--    print(self.data.wnd);
    mgrs.action:beginAction(self.button):MoveTo(t,pos):EaseBackOut():endAction();
end

function FuncButton:withDraw(t,pos)
--    print(self.data.wnd);
    mgrs.action:beginAction(self.button):MoveTo(t,pos):EaseBackOut():endAction();
end


local FuncWnd = class("TestWnd",BaseWnd);
function FuncWnd:ctor()
	FuncWnd.super.ctor(self);
    self.btns = {};
    self.menusIsExpand = false;
    self.btnWidth = 100;
    self.btnHeight = 100;
    self.allMenusParent = nil;
end

function FuncWnd:onOpen()
    self:addEvent();
    self:initBtns();
end

function FuncWnd:initBtns()
    self.allMenusParent = self:widget("right.allMenus");
    self.allMenusParent:setVisible(false);
    self.startMenu = self:widget("right.menu");
    local click = self:createClick({targetNode = self.startMenu,scale = 1.1});
    click:clickHandler(function()
        self.menusIsExpand = not self.menusIsExpand;
        if self.menusIsExpand then
            --展开
            mgrs.action:beginAction(self.startMenu):RotateBy(0.1,-90):EaseBackOut():endAction();
            self:dispatchEvent({name = enums.Event.ExpandMenu});
        else
            --收起
            mgrs.action:beginAction(self.startMenu):RotateBy(0.1,90):EaseBackOut():endAction();
            self:dispatchEvent({name = enums.Event.WithDrawMenu});
        end
    end);

    local vCount = 0;
    local hCount = 0;
    local downButtons = UiInfo.down;
    local vButtons = downButtons.V;
    for key, var in pairs(vButtons) do
        local id = var.id;
        if self:funcIsOpen(id) then
            vCount = vCount + 1;
            local btn = FuncButton.new(var,Direction.V);
            table.insert(self.btns,btn);
        end
    end

    local hButtons = downButtons.H;
    for key, var in pairs(hButtons) do
        local id = var.id;
        if self:funcIsOpen(id) then
            hCount = hCount + 1;
            local btn = FuncButton.new(var,Direction.H);
            table.insert(self.btns,btn);
        end
    end

    for key, var in pairs(self.btns) do
        var.button:setPosition(cc.p(0,0));
        self.allMenusParent:addChild(var.button);
    end
end

function FuncWnd:addEvent()
    self:addEventListener(enums.Event.ExpandMenu,handler(self,self.expandButton));
    self:addEventListener(enums.Event.WithDrawMenu,handler(self,self.withDrawButton));
end

function FuncWnd:expandButton()
    self:widget("aaaa"):setVisible(false);
    self:widget("right.allMenus"):setVisible(true);
    local vCount = 0;
    local hCount = 0;
    local pos = cc.p(0,0);
    local expandTime = 0.4;
    for key, var in pairs(self.btns) do
        local kind = var.kind;
        if kind == Direction.H then
            hCount = hCount + 1;
            pos = cc.p(-self.btnWidth * hCount,0);
        else
            vCount = vCount + 1;
            pos = cc.p(0,self.btnHeight * vCount);
        end
        var:expan(expandTime,pos);
    end
    mgrs.input:lock(enums.Locker.ExpanMenu,expandTime * 1000);
end

function FuncWnd:withDrawButton()
    self:widget("aaaa"):setVisible(true);
    self:widget("right.allMenus"):setVisible(false);
    local expandTime = 0.4;
    local pos = cc.p(0,0);
    for key, var in pairs(self.btns) do
        pos = cc.p(0,0);
        var:withDraw(expandTime,pos);
    end
    mgrs.input:lock(enums.Locker.ExpanMenu,expandTime * 1000);
end

function FuncWnd:funcIsOpen(id)
    return true;
end

function FuncWnd:onClose()
	
end

function FuncWnd:btnClicked()
    mgrs.log:printDebug("!!!!!TestWnd onClose!!!!!");
end



function FuncWnd:reqData(handler)
	handler(true);	
end

function FuncWnd:getRelativePosition()
	return {rx = 1; ry = 0; ox = mgrs.wnd.visibleRect.rightBottom.x; oy = mgrs.wnd.visibleRect.rightBottom.y;};
end

function FuncWnd:getLayer()
	return enums.Layer.Main;
end

function FuncWnd:getPreloadResList()
	return {};
end

function FuncWnd:getExclusiveType()
	return 0;
end

function FuncWnd:isOpenCloseSoundEnabled()
	return false;
end

function FuncWnd:onEnter()
	print("FuncWnd:onEnter");
end
function FuncWnd:onExit()
	print("FuncWnd:onExit");
end
function FuncWnd:onEnterTransitionFinish()
	print("FuncWnd:onEnterTransitionFinish");
end

function FuncWnd:onExitTransitionStart()
	print("FuncWnd:onExitTransitionStart");
end

function FuncWnd:onCleanup()
	print("FuncWnd:onCleanup");
end


mgrs.wnd:registerWnd(enums.Wnd.Func,FuncWnd);

--endregion
